# 测试写入word
from docx import Document
# 用于修改图片大小
from docx.shared import Cm
from docx.shared import RGBColor, Pt
# 中文字体设置
from docx.oxml.ns import qn
# 段落对齐
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

file_name = 'test-write.docx'
# 1. 创建Word
word = Document()

# 3. 添加标题
word.add_heading('一级标题', level=1)
# 4. 添加段落
p = word.add_paragraph('新的段落')
# 5. 添加文字块
p.add_run('加粗').bold = True
p.add_run('常规')
p.add_run('斜体').italic = True
# 6. 添加图片
word.add_picture(r'1.jpg')
# 修改大小
word.add_picture(r'1.jpg', width=Cm(70))
word.add_heading('二级标题', level=2)
word.add_heading('三级标题', level=3)
# 7. 添加分页
word.add_page_break()

# 设置样式
for p in word.paragraphs:
    # 段落样式设置
    p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
    for run in p.runs:
        run.font.name = 'Arial'
        run.font.bold = True  # 加粗
        run.font.italic = True  # 斜体
        run.font.underline = True  # 下划线
        run.font.strike = True  # 删除线
        run.font.shadow = True  # 阴影
        run.font.size = Pt(20)  # 字号
        run.font.color.rgb = RGBColor(255, 0, 0)  # 字体颜色

# 2. 保存文件
word.save(file_name)
